package com.fightingFish.user.dao.impl;


import com.fightingFish.user.bean.Anchor;
import com.fightingFish.user.bean.User;
import com.fightingFish.user.dao.AttentionDao;
import com.fightingFish.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AttentionDaoImpl implements AttentionDao {
    /**
     * 方法概述：调用方法查询该用户关注的普通用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<User> getAttention(int userId) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User userAll = null;
        ArrayList<User> list = new ArrayList<>();
        try {
            connection = JdbcUtil.getConnection();
            String sql = "select stateName,userid,nickName,photoImgUrl,anchorOrUser,state,createTime,balance,sex,signInfo\n" +
                    "signDay,userlevel,phone,email,veriCode,deadline from state s join (SELECT * FROM attention a join user u on a.beConcernedId = u.userId WHERE a.concernId=? \n" +
                    "and u.anchorOrUser is null) ua on s.stateId = ua.state";
            ps = connection.prepareStatement(sql);
            ps.setInt(1, userId);
            rs = ps.executeQuery();
            while (rs.next()) {
                userAll = new User();
                userAll.setUserId(rs.getInt("userid"));
                userAll.setNickName(rs.getString("nickName"));
                userAll.setPhotoImgUrl(rs.getString("photoImgUrl"));
                userAll.setSignDay(rs.getInt("signDay"));
                userAll.setUserLevel(rs.getInt("userLevel"));
                list.add(userAll);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(connection, ps, rs);
        }
        return list;
    }

    /**
     * 取消关注
     * @param concernId
     * @param beConcernedId
     * @return
     */
    @Override
    public boolean cancelAttention(int concernId, int beConcernedId) {
        Connection connection = null;
        PreparedStatement ps = null;
        int rows = 0;
        try {
            String sql = "DELETE FROM attention WHERE concernId =? and beConcernedId=?";
            connection = JdbcUtil.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1, concernId);
            ps.setInt(2, beConcernedId);
            rows = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(connection, ps, null);
        }
        return rows > 0;
    }



}
